
package com.eva.android.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.x52im.rbchat.R;

public abstract class DataLoadableFragment extends BaseFragment {

    /** "加载中"进度提示组件 */
    protected View loadingView = null;
    /** 界面内容显示父组件 */
    protected View contentView = null;

//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = initViews(inflater, container, savedInstanceState);
//
//        return view;
//    }

    @Override
    protected View initViews(@NonNull LayoutInflater inflater, @Nullable ViewGroup container){

        View view = inflater.inflate(R.layout.common_fragment_loading_layout, container, false);

        ViewStub loadingViewStub = view.findViewById(R.id.loadingViewStub);
        ViewStub contentViewStub = view.findViewById(R.id.contentViewStub);

        loadingViewStub.setLayoutResource(loadingLayout());
        loadingView = loadingViewStub.inflate();

        contentViewStub.setLayoutResource(contentLayout());
        contentView = contentViewStub.inflate();
        contentView.setVisibility(View.GONE);

//        initViewsAfter(view);

        return view;
    }

    protected abstract int contentLayout();

    protected int loadingLayout() {
        return R.layout.common_fragment_loading_progress;
    }

    protected void showContent() {
        if (contentView.getVisibility() == View.VISIBLE)
            return;
        loadingView.setVisibility(View.GONE);
        contentView.setVisibility(View.VISIBLE);
    }

    protected void showLoading() {
        if (loadingView.getVisibility() == View.VISIBLE)
            return;
        loadingView.setVisibility(View.VISIBLE);
        contentView.setVisibility(View.GONE);
    }
}
